package com.ylcode.libback.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylcode.libback.common.R;
import com.ylcode.libback.model.params.LendParam;
import com.ylcode.libback.model.pojo.Book;
import com.ylcode.libback.service.BookService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Resource
    private BookService bookService;

    /**
     * 查询所有书籍
     */
    @GetMapping
    public R<List<Book>> queryAll() {
        List<Book> bookList = bookService.list();
        return R.success(bookList);
    }

    /**
     * 查询书籍总数
     */
    @GetMapping("/total")
    public R<Long> total() {
        long total = bookService.count();
        log.info("总数" + total);
        return R.success(total);
    }

    /**
     * 书籍信息分页查询
     */
    @GetMapping("/page")
    public R<Page> page (Integer page, Integer pageSize, String name) {
        //创建分页插件
        Page<Book> pageInfo = new Page<>(page, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Book> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Book::getBookName, name);
        //添加排序条件
        lambdaQueryWrapper.orderByAsc(Book::getBookUploadTime);
        //执行查询
        bookService.page(pageInfo, lambdaQueryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 根据id查询书籍
     */
    @GetMapping("/{id}")
    public R<Book> queryBookById(@PathVariable Long id) {
        LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Book::getBookId, id);
        Book book = bookService.getOne(queryWrapper);
        return R.success(book);
    }

    /**
     * 根据作者或书名查询
     */
    @GetMapping("/bookNameOrAuthor")
    public R<List<Book>> queryBookByNameOrAuthor(String param) {
        if (param.equals("")) {
            return this.queryAll();
        }
        List<Book> books = bookService.selectByBookNameOrAuthor(param);
        if (books.size() == 0) {
            return R.error("未查到相关信息");
        }
        return R.success(books);
    }

    /**
     * 修改书籍信息
     */
    @PutMapping("/editBookInfo")
    public R<String> editBookInfo(@RequestBody Book book) {
        book.setBookUploadTime(LocalDateTime.now());
        boolean flag = bookService.updateById(book);
        if (!flag) {
            return R.error("修改异常");
        }
        return R.success("成功");
    }

    /**
     * 借书
     */
    @PutMapping("/lend")
    public R<String> lend(@RequestBody LendParam lendParam) {
        long bookIdL = Long.parseLong(lendParam.bookId());
        long userIdL = Long.parseLong(lendParam.userId());
        Boolean flag = bookService.lend(bookIdL, userIdL);
        if (!flag) {
            return R.error("没借成！");
        }
        return R.success("借成了");
    }


    /**
     * 添加书籍
     */
    @PostMapping("/add")
    public R<String> saveBook(@RequestBody Book book){
        log.info("demo666");
        book.setBookStatus("0");
        book.setBookUploadTime(LocalDateTime.now());
        bookService.save(book);
        return R.success("添加成功拉");
    }

    /**
     * 首页
     */
    @GetMapping("/home")
    public R<Page> home (Integer page, Integer pageSize, String name) {
        //创建分页插件
        Page<Book> pageInfo = new Page<>(page, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Book> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Book::getBookName, name);
        lambdaQueryWrapper.eq(Book::getBookStatus, "0");
        //添加排序条件
        lambdaQueryWrapper.orderByDesc(Book::getBookUploadTime);
        //执行查询
        bookService.page(pageInfo, lambdaQueryWrapper);
        return R.success(pageInfo);
    }

}
